package com.site.dao.impl;

import com.site.dao.OutStockDao;
import com.site.pojo.OutStock;
import com.site.utils.JDBCUtils;

import java.sql.Connection;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

/**
 * 出库表Dao实现类
 */
public class OutStockDaoImpl implements OutStockDao {
    //查询所有出库单
    @Override
    public List<OutStock> selectAll() {
        String sql = "select * from out_stock";
        List<OutStock> list = new ArrayList<OutStock>();
        ResultSet rs = null;
        Connection conn = null;
        try{
            conn = JDBCUtils.getConnection();
            rs = JDBCUtils.executeQuery(conn,sql);
            while (rs.next()){
                OutStock outStock = new OutStock();
                outStock.setOid(rs.getLong("oid"));
                outStock.setEid(rs.getLong("eid"));
                outStock.setGoodsId(rs.getLong("goods_id"));
                outStock.setOutStockNumber(rs.getString("out_stock_number"));
                outStock.setOutStockCount(rs.getInt("out_stock_count"));
                outStock.setOprTime(rs.getTimestamp("opr_time"));
                list.add(outStock);
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            JDBCUtils.close(rs,null,conn);
        }
        return list;
    }
    //通过id查询出库单
    @Override
    public OutStock selectByoid(Long oid) {
        String sql = "select * from out_stock where oid=?";
        OutStock outStock = new OutStock();
        ResultSet rs = null;
        Connection conn = null;
        try{
            conn = JDBCUtils.getConnection();
            rs = JDBCUtils.executeQuery(conn,sql,oid);
            while (rs.next()){
                outStock.setOid(rs.getLong("oid"));
                outStock.setEid(rs.getLong("eid"));
                outStock.setGoodsId(rs.getLong("goods_id"));
                outStock.setOutStockNumber(rs.getString("out_stock_number"));
                outStock.setOutStockCount(rs.getInt("out_stock_count"));
                outStock.setOprTime(rs.getTimestamp("opr_time"));
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            JDBCUtils.close(rs,null,conn);
        }
        return outStock;
    }
    //通过出库单编号查询出库单
    @Override
    public OutStock selectByOutStockNumber(String outStockNumber) {
        String sql = "select * from out_stock where out_stock_number=?";
        OutStock outStock = new OutStock();
        ResultSet rs = null;
        Connection conn = null;
        try{
            conn = JDBCUtils.getConnection();
            rs = JDBCUtils.executeQuery(conn,sql,outStockNumber);
            while (rs.next()){
                outStock.setOid(rs.getLong("oid"));
                outStock.setEid(rs.getLong("eid"));
                outStock.setGoodsId(rs.getLong("goods_id"));
                outStock.setOutStockNumber(rs.getString("out_stock_number"));
                outStock.setOutStockCount(rs.getInt("out_stock_count"));
                outStock.setOprTime(rs.getTimestamp("opr_time"));
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            JDBCUtils.close(rs,null,conn);
        }
        return outStock;
    }
    //无事务，新增出库单
    @Override
    public int addOutStock(OutStock outStock) {
        int num = 0;
        String sql = "insert into out_stock(goods_id,eid,out_stock_number,out_stock_count,opr_time)" +
                "values(?,?,?,?,?)";
        Object[] param = {outStock.getGoodsId(),outStock.getEid(),outStock.getOutStockNumber()
        ,outStock.getOutStockCount(),outStock.getOprTime()};
        try{
            num = JDBCUtils.executeUpdate(sql,param);
        }catch (Exception e){
            e.printStackTrace();
        }
        return num;
    }
    //有事务，新增出库单
    @Override
    public int addOutStock(Connection conn, OutStock outStock) {
        int num = 0;
        String sql = "insert into out_stock(goods_id,eid,out_stock_number,out_stock_count,opr_time)" +
                "values(?,?,?,?,?)";
        Object[] param = {outStock.getGoodsId(),outStock.getEid(),outStock.getOutStockNumber()
                ,outStock.getOutStockCount(),outStock.getOprTime()};
        try{
            num = JDBCUtils.executeUpdate(conn,sql,param);
        }catch (Exception e){
            e.printStackTrace();
        }
        return num;
    }
    //无事务，通过id删除出库单
    @Override
    public int deleteOutStockByoid(Long oid) {
        int num = 0;
        String sql = "delete from out_stock where oid=?";
        Object[] param = {oid};
        try{
            num = JDBCUtils.executeUpdate(sql,param);
        }catch (Exception e){
            e.printStackTrace();
        }
        return num;
    }
    //有事务，通过id删除出库单
    @Override
    public int deleteOutStockByoid(Connection conn, Long oid) {
        int num = 0;
        String sql = "delete from out_stock where oid=?";
        Object[] param = {oid};
        try{
            num = JDBCUtils.executeUpdate(conn,sql,param);
        }catch (Exception e){
            e.printStackTrace();
        }
        return num;
    }
    //无事务，通过出库单编号删除出库单
    @Override
    public int deleteOutStockByNumber(String number) {
        int num = 0;
        String sql = "delete from out_stock where out_stock_number=?";
        Object[] param = {number};
        try{
            num = JDBCUtils.executeUpdate(sql,param);
        }catch (Exception e){
            e.printStackTrace();
        }
        return num;
    }
    //有事务，通过出库单编号删除出库单
    @Override
    public int deleteOutStockByNumber(Connection conn, String number) {
        int num = 0;
        String sql = "delete from out_stock where out_stock_number=?";
        Object[] param = {number};
        try{
            num = JDBCUtils.executeUpdate(conn,sql,param);
        }catch (Exception e){
            e.printStackTrace();
        }
        return num;
    }
    //无事务，通过outStock修改出库单
    @Override
    public int updateOutStock(OutStock outStock) {
        int num = 0;
        String sql = "update out_stock set goods_id=?,eid=?,out_stock_number=?,out_stock_count=?,opr_time=?" +
                " where oid=?";
        Object[] param = {outStock.getGoodsId(),outStock.getEid(),outStock.getOutStockNumber()
        ,outStock.getOutStockCount(),outStock.getOprTime(),outStock.getOid()};
        try{
            num = JDBCUtils.executeUpdate(sql,param);
        }catch (Exception e){
            e.printStackTrace();
        }
        return num;
    }
    //有事务，通过oidutStock修改出库单
    @Override
    public int updateOutStock(Connection conn, OutStock outStock) {
        int num = 0;
        String sql = "update out_stock set goods_id=?,eid=?,out_stock_number=?,out_stock_count=?,opr_time=?" +
                " where oid=?";
        Object[] param = {outStock.getGoodsId(),outStock.getEid(),outStock.getOutStockNumber()
                ,outStock.getOutStockCount(),outStock.getOprTime(),outStock.getOid()};
        try{
            num = JDBCUtils.executeUpdate(conn,sql,param);
        }catch (Exception e){
            e.printStackTrace();
        }
        return num;
    }
}
